<template>
  <scroll-view
    scroll-y
    class="scroll-view flex-1"
    refresher-enabled
    enable-back-to-top
    :refresher-triggered="refresherTriggered"
    @refresherrefresh="onRefresherrefresh"
    @scrolltolower="getDataList"
  >
    <view class="common-container">
      <template v-if="dataList.length">
        <view
          v-for="item in dataList"
          :key="item.id"
          class="history-item"
          @click="uniNavigateTo(`/pages-sub/work-note/detail/index?id=${item.id}`)"
        >
          <view class="history-item-header">
            <view class="history-item-type">【{{ item.type_dictText }}】</view>
            <view class="history-item-time">{{ item.dateCode }}</view>
          </view>
          <view class="history-item-content">
            <view class="history-item-desc">本月工作情况：{{ item.workLog }}</view>
            <view class="history-item-desc">下月工作计划：{{ item.nextWorkLog }}</view>
          </view>
        </view>
      </template>
      <template v-else>
        <wd-status-tip image="content" tip="暂无内容" />
      </template>
    </view>
    <wd-loadmore
      v-if="dataList.length"
      finished-text="没有更多了"
      :state="loadState"
      @reload="getDataList"
    />
  </scroll-view>
</template>

<script setup>
import WorkApi from '@/service/work'
import { uniNavigateTo } from '@/utils'

const loadState = ref('loading')
const pageObj = ref({
  pageNo: 1,
  pageSize: 10,
})
const dataList = ref([])

const getDataList = async () => {
  if (loadState.value === 'finished') return
  try {
    const params = {
      ...pageObj.value,
      order: 'desc',
      column: 'createTime',
    }
    const { code, result } = await WorkApi.getWorkList(params)
    if (code === 200 && result) {
      const { records, pages } = result
      if (records?.length) {
        dataList.value = [...dataList.value, ...records]
      }
      if (pageObj.value.pageNo < pages) {
        pageObj.value.pageNo++
      } else {
        loadState.value = 'finished'
      }
    } else {
      loadState.value = 'error'
    }
  } catch (e) {
    loadState.value = 'error'
  }
}

const reset = () => {
  pageObj.value.pageNo = 1
  dataList.value = []
  loadState.value = 'loading'
}

const refreshList = () => {
  reset()
  getDataList()
}

const refresherTriggered = ref(false)
const onRefresherrefresh = async () => {
  refresherTriggered.value = true
  // 重置分页数据，刷新接口
  reset()
  await getDataList()
  refresherTriggered.value = false
}

onLoad(() => {
  refreshList()
})

defineExpose({
  refreshList,
})
</script>

<style lang="scss" scoped>
.scroll-view {
  width: 100%;
  height: calc(100vh - 46px);
}

.history-item {
  padding: $uni-spacing-col-lg $uni-spacing-row-lg;
  margin-bottom: $uni-spacing-col-lg;
  border-radius: $uni-border-radius-base;
  box-shadow: $uni-shadow-base;

  &-header {
    display: flex;
    align-items: center;
    padding-bottom: $uni-spacing-col-lg;
    margin-bottom: $uni-spacing-col-lg;
    font-weight: 600;
    border-bottom: 1px solid $uni-border-color;
  }

  &-type {
    color: $uni-color-primary;
  }

  &-desc {
    margin-bottom: $uni-spacing-col-base;
    font-size: $uni-font-size-base;
    color: $uni-text-color-grey;
  }
}
</style>
